<?php
// 数据库参数
$db_pram = array("localhost","root","123456","bbs");
$db=mysqli_connect($db_pram[0],$db_pram[1],$db_pram[2],$db_pram[3]);

// 封装增删改
function zsg($sql,$parm){

    // 声明要调用全局变量--方法外的变量
    global $db;
    $pre = $db->prepare($sql);

    // 用反射方式来给占位符赋值
    if($parm!=null and count($parm)>0){
        call_user_func_array(array($pre,"bind_param"),refvalue($parm));
    }
    return $pre->execute();
}

// 常规数组引用数组
function refvalue($arr){

    $refs = array();
    foreach ($arr as $k=>$v){
        $refs[$k]=&$arr[$k];
    }
    // 拼接数据类型
    $s = "";
    for ($i=0;$i<count($arr);$i++){
        $s.="s";
    }
    // 添加到数组最前面
    array_unshift($arr,$s);
    return $refs;
}


// 封装查询
function getList($sql,$parm){
    // 声明要调用全局变量--方法外的变量
    global $db;
    // 字段数组
    $parme = array();
    // 返回的结果集数组
    $results = array();
    $pre = $db->prepare($sql);
    if($parm!=null and count($parm)>0){
        call_user_func_array(array($pre,"bind_param"),refvalue($parm));
    }
     $pre->execute();
    // 返回结果集字段
    $meta = $pre->result_metadata();

    while ($field=$meta->fetch_field()){

        $parme[]=&$row[$field->name];
    }
    call_user_func_array(array($pre,"bind_result"),refvalue($parme));
    while ($pre->fetch()){
        $a=array();
        foreach ($row as $k=>$v){
            $a[$k]=$v;
        }
        $results[]=$a;
    }
    //关闭连接
    $pre->close();
    return $results;
}

//$sql = "insert into userinfo values(null,?,?,?,default,default)";
//
//$arr = array("sss","fff","ggg","123");
//$r =zsg($sql,$arr);
//var_dump($r);
//
//$sql = "select * from type ";
//$r1 = getList($sql,null);
//echo "<br><br><br>".$r1;
//var_dump($r1);